/*
 * app_src.c
 *
 *  Created on: Jun 29, 2025
 *      Author: User
 */

#include "app_isr.h"
#include "string.h"
#include "bsp_conf.h"

CAN_RxHeaderTypeDef RxHeader;
RxDataBuffer rxCan1DataBuffer[RECEIVEBUFFSIZE];
RxDataBuffer rxCan2DataBuffer[RECEIVEBUFFSIZE];
CAN_RxHeaderTypeDef Rx2Header;
uint8_t RxData[8];
uint8_t Rx2Data[8];
int fifo0_can1_flag = 0;
int fifo0_can2_flag = 0;
int fifo1_can2_flag = 0;
// BSP_XZC_DYZH
volatile int channelNum = 0;
int currentFrame = 0;
int totalFrame = 0;
int channelIndex = 0;
int dyzhIndex[7] = {0, 4, 8, 9, 11, 13, 15};
//市电0~3、取力4~7、绝缘8，充电组件9~10.供电组件1:11-12,供电组件2：13-14

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
	/* Get RX message */
	if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)
	{
		/* Reception Error */
		if (hcan->Instance == hcan1.Instance)
		{
			can1ReciveFlag ++;
		}
		else if(hcan->Instance == hcan2.Instance)
		{
			can2ReciveFlag ++;
		}
		Error_Handler();
	}
	if (hcan->Instance == hcan1.Instance)
	{
		/*与显控通信*/
 		if(RxHeader.ExtId ==  CONTROLFRAME ||RxHeader.ExtId == SETTINGFRAME||RxHeader.ExtId == READFRAME || RxHeader.ExtId == VERSIONFRAME)
		{
			fifo0_can1_flag = fifo0_can1_flag == RECEIVEBUFFSIZE ? 1 : fifo0_can1_flag + 1;
			rxCan1DataBuffer[fifo0_can1_flag - 1].RxHeaderID = RxHeader.ExtId;
			ErrorCan1ID.errortimes = 0;
			memcpy(rxCan1DataBuffer[fifo0_can1_flag - 1].RxData, RxData, 8);
		}
	}
	else if (hcan->Instance == hcan2.Instance)
	{
// 		if(RxHeader.ExtId == SETTINGBACKFRAME || RxHeader.ExtId == READBACKFRAME)
//		{
//			fifo0_can2_flag = fifo0_can2_flag == RECEIVEBUFFSIZE ? 1: fifo0_can2_flag + 1;
//			rxCan2DataBuffer[fifo0_can2_flag - 1].RxHeaderID = RxHeader.ExtId;
//			memcpy(rxCan2DataBuffer[fifo0_can2_flag - 1].RxData, RxData, 8);
//		}
//		else
//		{
//			Check_ReceiveFlag(RxHeader.ExtId);
// 			memcpy(dataBuffer[RxData[0]-1].data,RxData,8);
//		}
	}
}

void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
	/* Get RX message */
	if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO1, &Rx2Header, Rx2Data) != HAL_OK)
	{
		/* Reception Error */
		Error_Handler();
	}
	if (hcan->Instance == hcan1.Instance)
	{

	}
	else if (hcan->Instance == hcan2.Instance)
	{
		if((Rx2Header.ExtId == SETTINGBACKFRAME) || (Rx2Header.ExtId == READBACKFRAME) || (Rx2Header.ExtId == VERSIONCALLBACKFRAME))
		{
			fifo1_can2_flag = fifo1_can2_flag == RECEIVEBUFFSIZE ? 1: fifo1_can2_flag + 1;
			rxCan2DataBuffer[fifo1_can2_flag - 1].RxHeaderID = Rx2Header.ExtId;
			memcpy(rxCan2DataBuffer[fifo1_can2_flag - 1].RxData, Rx2Data, 8);
		}
		else
		{
#if XZC_DYZH_MODULE_ENABLED == 1
			uint32_t tempExtId = Rx2Header.ExtId;
			uint8_t temp = Rx2Data[0];
//			if((tempExtId == 0x0C306407) && temp == 0x07){
//				tempExtId = 0x0C306407;
//				temp = 0x11;
//			}
			uint8_t channelNum = tempExtId & 0x000000FF;
			uint8_t currentFrame = (temp&0xF0) >> 4;
			uint8_t channelIndex = dyzhIndex[channelNum-1] + currentFrame - 1;
			if(channelIndex<DATABUFFERSIZE)
			{
				memcpy(dataBuffer[channelIndex].data, Rx2Data, 8);
			}else{
				__NOP();
			}
#elif XZC_DYZH_MODULE_ENABLED == 0
			if((Rx2Header.ExtId&0x000000FF) == 0x00000080)
			{
				//故障检测报文
				memcpy(dataBuffer[DATABUFFERSIZE-2].data,Rx2Data,8);

			}else if((Rx2Header.ExtId&0x000000FF) == 0x00000081)
			{
				//风机调速板报文
				memcpy(dataBuffer[DATABUFFERSIZE-1].data,Rx2Data,8);

			}else{
				//通道运行状态报文
				memcpy(dataBuffer[Rx2Data[0] -1].data,Rx2Data,8);
			}
#endif
			Check_ReceiveFlag(Rx2Header.ExtId);
		}
	}
}
